package com.campusrecruitment.controller;

import com.campusrecruitment.common.JsonUtils;
import com.campusrecruitment.common.R;
import com.campusrecruitment.pojo.employer;
import com.campusrecruitment.pojo.resume;
import com.campusrecruitment.pojo.student;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;

/**
 * 学生应聘管理
 */
@Slf4j
@RestController
@RequestMapping("/student")
public class studentController {

    private ObjectMapper mapper = new ObjectMapper();

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping("/add")
    public R<student> addStudent(@RequestBody student student) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\student.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //得到单位信息集合
        List<student> students= mapper.readValue(jsonList,new TypeReference<List<student>>(){});

        //加入用户
        students.add(student);

        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(students);
        jsonUtils.writeJson(jsonString);

        return R.success(student);
    }

    /**
     * 通过名字查询学生信息
     * @param
     * @return
     */
    @PostMapping("/getStudent")
    public R<student> getStudent(@RequestBody resume resume) throws JsonProcessingException {
        String studentName=resume.getName();
        //初始化json工具
        JsonUtils jsonUtils = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\student.json"));
        String jsonList = jsonUtils.readJson(String.class);
        //得到单位信息集合
        List<student> students = mapper.readValue(jsonList, new TypeReference<List<student>>() {
        });

        for (student s : students
        ) {
            if (s.getName().equals(studentName)) {
                return R.success(s);
            }
        }
        return R.error("找不到该用户");
    }

    /**
     * 查询登录学生信息
     * @return
     */
    @PostMapping("/init")
    public R<student> init(@RequestBody student student) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\student.json"));
        String jsonList = jsonUtils.readJson(String.class);
        //得到单位信息集合
        List<student> students = mapper.readValue(jsonList, new TypeReference<List<student>>() {
        });

        String name = student.getName();
        for (student s : students
        ) {
            if (s.getName().equals(name)) {
                return R.success(s);
            }
        }
        return R.error("找不到该用户");
    }
    /**
     * 修改登录用户信息
     * @param
     */
    @PostMapping("/update")
    public R<student> update(@RequestBody student student) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\student.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //得到单位信息集合
        List<student> students= mapper.readValue(jsonList,new TypeReference<List<student>>(){});

        String name=student.getName();
        for (int i=0;i<students.size();i++
        ) {
            if (students.get(i).getName().equals(name)){
                //更新该用户
                students.get(i).setId(student.getId());
                students.get(i).setName(student.getName());
                students.get(i).setAge(student.getAge());
                students.get(i).setAddress(student.getAddress());
                students.get(i).setGraduation(student.getGraduation());
                students.get(i).setPhone(student.getPhone());
                students.get(i).setMajor(student.getMajor());
                students.get(i).setSelfEvaluation(student.getSelfEvaluation());
                break;
            }
        }
        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(students);
        jsonUtils.writeJson(jsonString);
        return R.success(student);
    }

}
